<?php
	/**
	 * 标签类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	class tpl_tag_define
	{
		var $database = null;
		var $tag_file_root = '';

		/**
		 * description
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function tpl_tag_define(  )
		{
			$this->database = load( 'database' );
			$this->tag_file_root = config( 'biz_dir' ) . 'taglib/';
		}


		//返回全部标签定义
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 9999;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : 't1.id asc';
			$tpl_id = isset( $data['tpl_id'] ) ? $data['tpl_id'] : 0;

			$where = ' ';
			if ( $tpl_id != 0 )
			{
				$where .= ' and tpl_id=' . $tpl_id;
			}

			$count = 'from tpl_tag_define t1,tag t2 where t1.tag_id=t2.id ' . $where;
			$list = 'select * ' . $count . ' order by ' . $order;

			$result = $this->database->split( $count, $list, $size, $page );

			return $result;
		}


		//获取某条标签定义
		function get( $data )
		{
			$result = array();
			$where = ' where 1=1 ';

			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$where .= ' and id=' . $data['id'];
			}

			$result = $this->database->unique( 'select * from tpl_tag_define ' . $where );

			return $result;
		}


		//修改标签定义
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['id']['kind'] = '';
			$field['id']['type'] = 'num';
			$field['id']['min'] = '1';
			$field['id']['max'] = '8';
			$field['id']['info'] = '请传递ID';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{

				$this->database->start();

				$this->database->set( 'tpl_tag_define', $data, array( 'id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}


		//添加新的标签定义
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() , 'id' => 0 );
			$validation = load( 'validation' );
			$field = array();

			$field['tag']['kind'] = '';
			$field['tag']['type'] = 'text';
			$field['tag']['min'] = '1';
			$field['tag']['max'] = '255';
			$field['tag']['info'] = '标签定义不能为空';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$data = json_decode( urldecode( stripslashes( $data['tag'] ) ), true );

				//检查此模板是否为引用模板，若是则不能添加标签定义
				$sql = "select refer_tpl_id from tpl where id=" . $data['tpl_id'];
				$tmp = $this->database->unique( $sql );
				if ( isset( $tmp['refer_tpl_id'] ) )
				{
					$result['message'][] = '该模板是引用其它模板，不能单独定义标签。';
					return $result;
				}

				//检查是否重复
				$sql = "select var_name from tpl_tag_define where var_name='" . $data['var_name'] . "' ";
				$tmp = $this->database->unique( $sql );
				if ( isset( $tmp['var_name'] ) )
				{
					$result['message'][] = '变量名重复';
					return $result;
				}

				$tag_file = $this->tag_file_root . $data['tagtype'] . '.php';
				if ( ! is_file( $tag_file ) )
				{
					$result['message'][] = '标签类文件不存在.' . $tag_file;
					return $result;
				}

				//查寻tag_id
				$sql = "select id from tag where class_name='" . $data['tagtype'] . "' ";
				$tmp = $this->database->unique( $sql );
				if ( ! isset( $tmp['id'] ) )
				{
					$result['message'][] = '标签类不存在';
					return $result;
				}

				$effected = 0;
				$data['tag_id'] = $tmp['id'];
				$this->database->start();

				$sql = "insert into tpl_tag_define ( tpl_id, tag_id, var_name, argument ) values ( '". $data['tpl_id'] . "', '". $data['tag_id'] . "', '". $data['var_name'] . "', '". json_encode( $data['argument'] ) . "'  ) ";
				$id = $this->database->command( $sql );

				if ( $this->database->end() )
				{
					$result['id'] = $id;
					$result['success'] = true;
				}
				else
				{
					$result['message'][] = '添加失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}


		//删除具体配置的数据
		function del( $data )
		{
			$result = array( 'success' => 0 );
			if ( empty( $data ) || count( $data ) <= 0 )
			{
				return $result;
			}
			$where = ' where 1=1 ';
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$where .= ' and id = ' . $data['id'];
			}

			$effected = $this->database->command( 'delete from tpl_tag_define ' . $where );
			if ( $effected > 0 )
			{
				$result['success'] = 1;
			}

			return $result;
		}


		/**
		 * 根据条件判断记录是否唯一
		 * detail description
		 * @param integer name
		 * @return boolean
		 */
		function is_dup( $name )
		{
			$result = $this->database->unique( "select id from tpl_tag_define where name='" . $name . "' " );
			return isset( $result['id'] ) ? true : false;
		}

	}
?>